const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const _ = db.command
const $ = db.command.aggregate
const ZXOrder = db.collection('ZX-Order')
const ZXBooth = db.collection('ZX-Booth')

const nowDate = db.serverDate()

// 获取openId云函数入口函数
exports.main = async (event, context,token,gameId) => {
    process.env.TZ = 'Asia/Shanghai'
    
    var api = event.api
    const goodsName = event.data.goodsName
    
    // 订单状态
    // 0待交易。(24小时时间，减少寄售数量)
    // -1订单关闭。(释放寄售数量)
    // 1交易成功。()
    // 666 已发货

    // 添加 api-order-add
    if(api=='api-order-add'){
        const dataM = event.data.data
        
        dataM.order_num = nowDate;
        dataM.createTime = nowDate;
        dataM.close_time = nowDate + 48*60*60*1000;
        dataM.order_status = '0'

        // 更新 booth 寄售数量
        // const bNumber = dataM.oNumber
        await ZXBooth.doc(dataM.boothId).update({
            number: _.inc(-dataM.oNumber)
        })
        return await ZXOrder.add(event.data.data)
    }

    // 更新订单
    if(api=='api-order-update'){
        var params = {}
        var order_status = event.data.order_status
        var boothId = event.data.boothId
        var oNumber = event.data.oNumber
        var _id = event.data._id

        if(_id){
            if(order_status)
                params.order_status = order_status
            
            if(order_status=='-1'&&oNumber)
                await ZXBooth.doc(boothId).update({
                    number: _.inc(oNumber)
                })

            return await ZXOrder.doc(_id).update(params)
        }
    }

    // 我的订单列表
    if(api=='api-order-list-buy'){
        var params = {}
        if(goodsName)
            params.goodsName = goodsName
        params.buyId = token
        if(token){
            const res = await db
            .collection('ZX-Order')
            .aggregate()
            .match(params)
            .lookup({
                from: 'ZX-Data',
                localField: 'goodsId',
                foreignField: '_id',
                as: 'goodsList'
            })
            .lookup({
                from: 'ZX-Booth',
                localField: 'boothId',
                foreignField: '_id',
                as: 'boothList'
            })
            .lookup({
                from: 'ZX-User',
                localField: 'sellId',
                foreignField: '_id',
                as: 'sellList'
            })
            .skip((event.data.pageIndex - 1) * event.data.pageSize)
            .limit(event.data.pageSize)
            .end()
            const list = res.data

            const total = await db
            .collection('ZX-Order')
            .aggregate()
            .match(params)
            .count('total')
            .end()

            var tatalCount = 0
            if(total.data&&total.data.length>0){
                tatalCount = total.data[0].total
            }

            return {
                list: list,
                total: tatalCount
            }
        }
    }

    // 我卖了的
    if(api=='api-order-list-sell'){
        var params = {}
        if(goodsName)
            params.goodsName = goodsName
        params.sellId = token
        if(token){
            const res = await db
            .collection('ZX-Order')
            .aggregate()
            .match(params)
            .lookup({
                from: 'ZX-Booth',
                localField: 'boothId',
                foreignField: '_id',
                as: 'boothList'
            })
            .lookup({
                from: 'ZX-Data',
                localField: 'goodsId',
                foreignField: '_id',
                as: 'goodsList'
            })
            .lookup({
                from: 'ZX-User',
                localField: 'buyId',
                foreignField: '_id',
                as: 'buyList'
            })
            .lookup({
                from: 'ZX-User',
                localField: 'sellId',
                foreignField: '_id',
                as: 'sellList'
            })
            .skip((event.data.pageIndex - 1) * event.data.pageSize)
            .limit(event.data.pageSize)
            .end()

            const list = res.data

            const total = await db
            .collection('ZX-Order')
            .aggregate()
            .match(params)
            .count('total')
            .end()

            var tatalCount = 0
            if(total.data&&total.data.length>0){
                tatalCount = total.data[0].total
            }

            return {
                list: list,
                total: tatalCount
            }
        }
    }

    
};